import BitmapFont = egret.BitmapFont;
import BitmapText = egret.BitmapText;
/**
 * Create by richliu1023
 * @date 2016-07-04
 * @email richliu1023@gmail.com
 * @github https://github.com/RichLiu1023
 * @description a simple explanation
 */
class UICountDown extends egret.DisplayObjectContainer {

	private _countDown:egret.BitmapText;
	private _maskBg:egret.Shape;
	private _callback:Function;
	private _thisObj:any;

	public constructor() {
		super();
		this._maskBg = new egret.Shape();
		this._maskBg.touchEnabled = true;
		this._maskBg.graphics.clear();
		this._maskBg.graphics.beginFill( 0x000000, 0.5 );
		this._maskBg.graphics.drawRect( 0, 0, GameConfig.STAGE_W, GameConfig.STAGE_H );
		this._maskBg.graphics.endFill();
		this.addChild( this._maskBg );

		this._countDown = new egret.BitmapText();
		this._countDown.font = RES.getRes( 'number3_fnt' );
		this._countDown.x = GameConfig.STAGE_W / 2;
		this._countDown.y = GameConfig.STAGE_H / 2;
		this._countDown.anchorOffsetX = 63;
		this._countDown.anchorOffsetY = 81;
		this.addChild( this._countDown );
	}

	public show( num:number ):void {
		Tween.removeTweens( this._countDown );
		UIController.topLayer.addChild( this );
		var tween = Tween.get( this._countDown )
		while ( num-- > 0 ) {
			tween.to( { scaleX: 3, scaleY: 3, alpha: 0, text: (num + 1).toString() } )
				.to( { scaleX: 1, scaleY: 1, alpha: 1 }, 233 )
				.wait( 600 ).to( { alpha: 0 }, 163 );
		}
		tween.call( this.onComplete, this );
	}

	private reset( num:number ):void {
		this._countDown.scaleX = this._countDown.scaleY = 3.0;
		this._countDown.text = num.toString();
		this._countDown.alpha = 1;
	}

	private onComplete():void {
		this._callback && this._callback.call( this._thisObj );
		UIController.topLayer.removeChild( this );
	}

	set callback( value:Function ) {
		this._callback = value;
	}

	set thisObj( value:any ) {
		this._thisObj = value;
	}
}